package ${artifactId}.controller.${moduleName};

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.ahccc.common.util.StrUtil;
import com.ahccc.common.util.bootStrapTable.PageVO;
import com.ahccc.common.ExecuteState;
import com.ahccc.common.ResponseData;
import com.ahccc.common.ResultMsg;
import com.ahccc.common.BaseController;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import ${artifactId}.model.${moduleName}.${name};
import ${artifactId}.service.${moduleName}.${name}Manager;
import java.util.List;
/**
* @copyright:${copyright}
* @Author:${author}
* @Date:${datetime}
* @Description:
*/
@Controller
@RequestMapping("${sname}")
public class ${name}Controller extends BaseController{

	@Resource
	private ${name}Manager manager;

	/**
	 * 按指定id的查询信息
	 *
	 * @param ${pk.simpleName}
	 * @return
	 */
	@GetMapping(value = "/{${pk.simpleName}}")
	@ResponseBody
	public ResponseData get(@PathVariable() ${pk.javaType} ${pk.simpleName}) {
		try{
			return new ResponseData(ExecuteState.SUCCESS, ResultMsg.succeed, manager.get(${pk.simpleName}));
		} catch (Exception e) {
			return new ResponseData(ExecuteState.ERROR, e.getMessage(), ${pk.simpleName});
		}
	}

	/**
	 * 修改或新增
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/save", method = { RequestMethod.PUT, RequestMethod.POST })
	@ResponseBody
	public ResponseData save(@RequestBody ${name} ${sname}) {
		try{
			manager.save(${sname});
			return new ResponseData(ExecuteState.SUCCESS, ResultMsg.saveSucceed, ${sname});
		} catch (Exception e) {
			return new ResponseData(ExecuteState.ERROR, e.getMessage(), ${sname});
		}
	}
	<#--public ResponseData save(HttpServletRequest request) {-->
		<#--${pk.javaType} ${pk.simpleName}=null;-->
		<#--${name} vo = new ${name}();-->
		<#--String parameter${pk.fixCode}=request.getParameter("${pk.simpleName}");-->
		<#--if (!StringUtils.isEmpty(parameter${pk.fixCode})) {-->
			<#--${pk.simpleName} = ${pk.javaType}.valueOf(parameter${pk.fixCode});-->
		<#--}-->
		<#--try{-->
			<#--if (!StringUtils.isEmpty(${pk.simpleName})) {-->
				<#--vo = manager.get(${pk.simpleName});-->
			<#--}-->
			<#--bind(request, vo);-->
			<#--manager.save(vo);-->
			<#--return new ResponseData(ExecuteState.SUCCESS, ResultMsg.saveSucceed, vo);-->
		<#--} catch (Exception e) {-->
			<#--return new ResponseData(ExecuteState.ERROR, e.getMessage(), vo);-->
		<#--}-->
	<#--}-->

	/**
	* 修改或新增
	* @param
	* @return
	*/
	@RequestMapping(value = "/update", method = { RequestMethod.PUT, RequestMethod.POST })
	@ResponseBody
	public ResponseData update(@RequestBody ${name} ${sname}) {
		try{
			manager.save(${sname});
			return new ResponseData(ExecuteState.SUCCESS, ResultMsg.saveSucceed, ${sname});
		} catch (Exception e) {
			return new ResponseData(ExecuteState.ERROR, e.getMessage(), ${sname});
		}
	}


     /**
	 * 删除记录 单条
	 */
	@DeleteMapping(value = "/delete/{id}")
	@ResponseBody
	public ResponseData delete(@PathVariable() <#list pks as f>${f.javaType}</#list> id) {
		try {
			manager.remove(id);
			return new ResponseData(ExecuteState.SUCCESS, ResultMsg.delSucceed, id);
		} catch (Exception e) {
			return new ResponseData(ExecuteState.ERROR, e.getMessage(), id);
		}
	}

    /**
    * 删除记录 多条
    */
	@PostMapping(value = "/batchRemove")
	@ResponseBody
	public ResponseData batchRemove(@RequestParam("ids[]")  <#list pks as f>${f.javaType}</#list>[] ids) {
		try {
			manager.remove(ids);
			return new ResponseData(ExecuteState.SUCCESS, ResultMsg.delSucceed, ids);
		} catch (Exception e) {
			return new ResponseData(ExecuteState.ERROR, e.getMessage(), ids);
		}
	}

	/**
	* 删除记录 多条
	*/
	@PostMapping(value = "/remove")
	@ResponseBody
	public ResponseData remove(@RequestBody List<${pk.javaType}> ids) {
		try {
			${pk.javaType}[] array = new ${pk.javaType}[ids.size()];
			${pk.javaType}[] s=ids.toArray(array);
			manager.remove(array);
			return new ResponseData(ExecuteState.SUCCESS, ResultMsg.delSucceed, ids);
		} catch (Exception e) {
			return new ResponseData(ExecuteState.ERROR, e.getMessage(), ids);
		}
	}

	/** 默认动态查询 */
	@GetMapping(value = "/list")
	@ResponseBody
	public PageVO list(HttpServletRequest request,Integer pageNumber,Integer pageSize,String sortName,String sortOrder) {
		${name} vo = new ${name}();
		bind(request, vo);
		Sort sort=new Sort(Sort.Direction.ASC,"${pk.simpleName}");
		if(!StrUtil.isEmpty(sortName)&&!StrUtil.isEmpty(sortOrder)){
			sort=new Sort("asc".equals(sortOrder)?Sort.Direction.ASC:Sort.Direction.DESC,sortName);
		}
		return manager.findAll(vo,  sort,  pageNumber,  pageSize);
	}

	/** 默认动态查询 */
	@GetMapping(value = "/page")
	@ResponseBody
	public ResponseData page(HttpServletRequest request, Integer current, Integer size, String sortName, String sortOrder) {
		try {
			${name} vo = new ${name}();
			bind(request, vo);
			Sort sort=new Sort(Sort.Direction.ASC,"${pk.simpleName}");
			if(!StrUtil.isEmpty(sortName)&&!StrUtil.isEmpty(sortOrder)){
			sort=new Sort("asc".equals(sortOrder)?Sort.Direction.ASC:Sort.Direction.DESC,sortName);
		}
			return new ResponseData(ExecuteState.SUCCESS, ResultMsg.succeed, manager.pagingByVue(vo,  sort,  current,  size));
		} catch (Exception e) {
			return new ResponseData(ExecuteState.ERROR, e.getMessage(), "");
		}
	}
}
